"""
    Coupon contoller.
"""

from datetime import date
from turbogears import widgets, validators, identity
from mnemoply import model
from mnemoply.mass_actions import MassAction, MassActionForm
from mnemoply.controllers.lookup import LookupController, TableCol, TableLookupDataSource
from mnemoply.controllers.crud import CRUDController
from mnemoply.widgets import DictionaryField, StudentInGroupField


class MAApplyCoupon(MassAction):
    target_class = model.Coupon
    name = "apply_coupon"
    title = _("Apply coupon")

    form = MassActionForm(
        name=name,
        fields=[
            StudentInGroupField(
                name="student_in_group_id",
                model_object_name='StudentInGroup',
                not_empty=True,
                label=_("Student")),
            ])

    def call(self, objs, student_in_group_id):
        studentInGroup = model.StudentInGroup.get(int(student_in_group_id))
        agreement = studentInGroup.studentAgreement
        for c in objs:
             if not c.Apply(agreement):
                 self.flash_message = _("Not applied for all records.")
             

class MAChangeType(MassAction):
    target_class = model.Coupon
    name = "change_type"
    title = _("Change type")

    form = MassActionForm(
        name=name,
        fields=[
            DictionaryField(
                name="type_id",
                model_object_name='CouponType',
                not_empty=True,
                label=_("Coupon type")),
            ])

    def call(self, objs, type_id):
        type = model.CouponType.get(int(type_id))

        for c in objs:
            c.type = type

class MAChangeDates(MassAction):
    target_class = model.Coupon
    name = "change_dates"
    title = _("Change dates")

    form = MassActionForm(
        name=name,
        fields=[
            widgets.CalendarDatePicker(
                label = _('Date begin'),
                name = 'date_begin',
                calendar_lang = 'ru',
                format = '%d.%m.%Y',
                validator = validators.DateTimeConverter(format="%d.%m.%Y")),
            widgets.CalendarDatePicker(
                label = _('Date end'),
                name = 'date_end',
                calendar_lang = 'ru',
                format = '%d.%m.%Y',
                validator = validators.DateTimeConverter(format="%d.%m.%Y")),
            ]
        )

    def call(self, objs, date_begin, date_end):
        for c in objs:
            c.dateBegin = date_begin
            c.dateEnd = date_end

class MACancel(MassAction):
    target_class = model.Coupon
    name = "cancel"
    title = _("Cancel")

    form = MassActionForm(
        name=name,
        fields=[
            widgets.TextField(
                name="comment",
                validator=validators.NotEmpty(),
                label="Comment"
            )
        ]
    )

    def call(self, objs, comment):
        for c in objs:
            c.isCancel = True
            c.cancellationDate = date.today()
            c.comment = comment
            c.owner = identity.current.user.person

class CouponController(LookupController, CRUDController):
    model_object = model.Coupon
    lookup_columns = [
        TableCol(name='id', caption=_('Number')),
        TableCol(name='coupon_type', caption=_('Coupon type')),
        TableCol(name='state', caption=_('State')),
        TableCol(name='who_received', caption=_('Who received')),
        TableCol(name='who_used', caption=_('Who used')),
        TableCol(name='date_used', caption=_('Date used')),
        TableCol(name='is_cancel_str', caption=_('Is cancel?'), hidden=True),
        TableCol(name='date_begin', caption=_('Date begin'), hidden=True),
        TableCol(name='date_end', caption=_('Date end'), hidden=True),
        TableCol(name='temporary_payment_record', caption=_('Temporary payment record'), hidden=True),
        TableCol(name='cancellation_date', caption=_('Cancellation date'), hidden=True),
        TableCol(name='owner', caption=_('Owner'), hidden=True),
        TableCol(name='comment', caption=_('Comment'), hidden=True),
    ]
    view_fields = [
        'dateBegin',
        'dateEnd',
        'temporaryPaymentRecordID',
        'studentAgreementID',
        'typeID',
        'isCancel',
        'cancellationDate',
        'ownerID',
        'comment'
    ]

    edit_fields = []

    def __init__(self):
        LookupController.__init__(self,
                                  data_source=TableLookupDataSource('coupon_lookup'),
                                  columns=self.lookup_columns,
                                  title=self.model_object.model_title,
                                  mass_actions_class=self.model_object,
                                  )
        CRUDController.__init__(self, model_object=self.model_object)
        self.local_menu = []
        self.object_local_menu = []

exposed = CouponController()
